package com.taxi.driver.module.order.address;

import android.text.TextUtils;

import com.amap.api.services.core.AMapException;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;
import com.qianxx.utils.TypeUtils;
import com.taxi.driver.R;
import com.taxi.driver.common.BasePresenter;
import com.taxi.driver.data.user.UserRepository;
import com.taxi.driver.module.vo.AddressVO;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import static com.amap.api.services.poisearch.PoiSearch.Query;

/**
 * 功能描述：
 */
public class ChangeAddrPresenter extends BasePresenter implements ChangeAddrContract.Presenter, PoiSearch.OnPoiSearchListener {

    ChangeAddrContract.View mView;
    UserRepository mUserRepository;
    private PoiSearch.Query query;// Poi查询条件类

    @Inject
    public ChangeAddrPresenter(ChangeAddrContract.View view, UserRepository userRepository) {
        mView = view;
        mUserRepository = userRepository;
    }

    @Override
    public String getCurrentCity() {
        String city = mUserRepository.getCity();
        if (TextUtils.isEmpty(city)) city = "厦门";
        return city;
    }

    @Override
    public String getProvince() {
        String province = mUserRepository.getProvince();
        if (TextUtils.isEmpty(province)) province = "福建";
        return province;
    }

    @Override
    public List<AddressVO> getHistoryAddr() {
        return mUserRepository.getHistoryAddr();
    }

    @Override
    public void searchAddr(String city, String keyword) {
        query = new Query(keyword, "", TypeUtils.getValue(city));
        query.setPageSize(10);// 设置每页最多返回多少条poiitem
        query.setPageNum(0);//设置查询页码
        PoiSearch poiSearch = new PoiSearch(mView.getContext(), query);
        poiSearch.setOnPoiSearchListener(this);
        poiSearch.searchPOIAsyn();
    }

    @Override
    public void onPoiSearched(PoiResult result, int rCode) {
        if (rCode == AMapException.CODE_AMAP_SUCCESS) {
            if (result != null && result.getQuery() != null) {// 搜索poi的结果
                if (result.getQuery().equals(query)) {// 是否是同一条
                    List<AddressVO> list = new ArrayList<>();
                    // 取得搜索到的poiitems有多少页
                    List<PoiItem> poiItems = result.getPois();// 取得第一页的poiitem数据，页数从数字0开始
                    if (poiItems != null && poiItems.size() > 0) {
                        for (PoiItem item : poiItems) {
                            AddressVO vo = new AddressVO();
                            vo.setAddress(item.getTitle());
                            vo.setAddressDetail(item.getSnippet());
                            LatLonPoint point = item.getLatLonPoint();
                            vo.setLat(point.getLatitude());
                            vo.setLng(point.getLongitude());
                            list.add(vo);
                        }
                    }
                    mView.showSearchedAddr(list);
                    if (list.isEmpty()) mView.toast(R.string.order_search_no_result);
                }
            } else {
                mView.toast(R.string.order_search_no_result);
            }
        } else {
            mView.toast(R.string.order_search_error);
        }
    }

    @Override
    public void onPoiItemSearched(PoiItem poiItem, int rCode) {

    }

}
